

from rest_framework import serializers
from orders.models import OrderInfo,OrderGoods
from goods.models import SKU

# SKU序列化器
class SKUSimpleModelSerializer(serializers.ModelSerializer):
    class Meta:
        model = SKU
        fields = [
            'name',
            'default_image'
        ]


# 订单商品序列化器
class OrderGoodsModelSerializer(serializers.ModelSerializer):
    # sku是当前订单商品OrderGoods对象关联的单一SKU主表对象
    sku = SKUSimpleModelSerializer()

    class Meta:
        model = OrderGoods
        fields = [
            'count',
            'price',
            'sku'
        ]


# 订单详情序列化器
class OrderDetailModelSerializer(serializers.ModelSerializer):
    user = serializers.StringRelatedField()
    # skus记录和表达，当前订单关联的多个从表OrderGoods对象
    skus = OrderGoodsModelSerializer(many=True)

    class Meta:
        model = OrderInfo
        fields = [
            'order_id',
            'user',
            'total_count',
            'total_amount',
            'freight',
            'pay_method',
            'status',
            'create_time',

            'skus',
        ]



# 订单简单序列化器
class OrderSimpleModelSerializer(serializers.ModelSerializer):
    # DateTimeField类型，默认序列化结果为iso标准时间字符串格式：2021-04-27T10:56:55.861885+08:00
    # 通过其约束条件format来自定义日期输出字符串格式
    create_time = serializers.DateTimeField(format='%Y/%m/%d')

    class Meta:
        model = OrderInfo
        fields = [
            'order_id',
            'create_time'
        ]


